package reference;

import org.junit.jupiter.api.Test;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

/**
 * <描述>
 *
 * @author timothy.yang cloudwise
 * @since 2022-11-20 11:36
 */
public class ReferenceTest {
    @Test
    public void weakReference() throws Exception {
        Car car = new Car();
        System.out.println(car);
        WeakReference<Car> car2 = new WeakReference<>(car);
        car = null;
        System.out.println(car);
        System.gc();
        Thread.sleep(1000L);
        System.out.println(car2.get());
    }

    public void softReference() {
        Car car = new Car();
        System.out.println(car);
        WeakReference<Car> car2 = new WeakReference<>(car);
        car = null;
        System.out.println(car);
        System.out.println(car2.get());
    }

}
